package com.mfin.bs.services;

import com.mfin.bs.mgr.impl.TransactionManager;
import com.mfin.bs.mgr.interfaces.IFinanceInformationManager;
import com.mfin.bs.mgr.interfaces.ILoanMasterManager;
import com.mfin.bs.mgr.interfaces.ITransactionManager;
import com.mfin.enumeration.LoanType;
import com.mfin.vo.finance.FinanceVO;
import com.mfin.vo.loan.FinanceLoanVO;
import com.mfin.vo.loan.LoanApplicationVO;
import com.mfin.vo.loan.LoanMasterVO;
import com.mfin.vo.loan.LoanPreferenceListVO;
import com.mfin.vo.loan.LoanPreferenceVO;

public class LoanMasterServicesImpl extends BaseImpl implements
		ILoanMasterServices {

	@Override
	public FinanceLoanVO createLoan(FinanceVO financeVO,
			LoanApplicationVO loanApplicationVO) {
		ITransactionManager transaction = new TransactionManager();
		transaction.beginTransaction();

		ILoanMasterManager loanMasterManager = getLoanMasterManager(transaction
				.getPersistenceGovernor());
		FinanceLoanVO loan = loanMasterManager.createLoan(financeVO,
				loanApplicationVO);

		LoanType loanType = loan.getLoanType();

		if (LoanType.CD.equals(loanType)) {

		} else if (LoanType.CCD.equals(loanType)) {

		} else if (LoanType.STBD.equals(loanType)) {

		} else if (LoanType.TBD.equals(loanType)) {

		} else if (LoanType.SPL.equals(loanType)) {

		} else {
		}

		transaction.commitTransaction();
		return loan;
	}

	@Override
	public LoanPreferenceListVO getFinanceLoanPreferences(FinanceVO financeVO) {
		ITransactionManager transaction = new TransactionManager();
		transaction.beginTransaction();

		IFinanceInformationManager financeInformationManager = getFinanceManager(transaction
				.getPersistenceGovernor());
		ILoanMasterManager loanMasterManager = getLoanMasterManager(transaction
				.getPersistenceGovernor());

		financeVO = financeInformationManager.getFinance(financeVO);

		LoanPreferenceListVO loanPreferenceListVO = null;
		LoanType[] loanTypes = loanMasterManager.getAllLoanTypes();

		if (loanTypes != null) {
			LoanPreferenceVO[] loanPreferences = new LoanPreferenceVO[loanTypes.length];
			int index = 0;
			for (LoanType loanType : loanTypes) {
				loanPreferences[index++] = loanMasterManager.getLoanPreference(
						financeVO, loanType);
			}
			loanPreferenceListVO = new LoanPreferenceListVO(
							loanPreferences);
		}

		transaction.commitTransaction();
		return loanPreferenceListVO;
	}

	@Override
	public LoanPreferenceVO getFinanceLoanPreference(FinanceVO financeVO,
			LoanType loanType) {
		ITransactionManager transaction = new TransactionManager();
		transaction.beginTransaction();

		IFinanceInformationManager financeInformationManager = getFinanceManager(transaction
				.getPersistenceGovernor());
		ILoanMasterManager loanMasterManager = getLoanMasterManager(transaction
				.getPersistenceGovernor());

		financeVO = financeInformationManager.getFinance(financeVO);

		LoanPreferenceVO loanPreferenceVO = loanMasterManager
				.getLoanPreference(financeVO, loanType);

		transaction.commitTransaction();
		return loanPreferenceVO;
	}

	@Override
	public LoanMasterVO addLoanMaster(LoanMasterVO loanMasterVO) {
		ITransactionManager transaction = new TransactionManager();
		transaction.beginTransaction();

		ILoanMasterManager loanMasterManager = getLoanMasterManager(transaction
				.getPersistenceGovernor());

		LoanMasterVO updateLoanMasterVO = loanMasterManager.addLoanMaster(loanMasterVO);

		transaction.commitTransaction();
		return updateLoanMasterVO;
	}

	@Override
	public void addFinanceLoanPreference(FinanceVO financeVO,
			LoanPreferenceVO loanPreferenceVO) {
		logMethodBegin("FinanceServicesImpl", "addFinanceLoanPreference");

		ITransactionManager transaction = new TransactionManager();
		transaction.beginTransaction();

		ILoanMasterManager loanMasterManager = getLoanMasterManager(transaction
				.getPersistenceGovernor());
		loanMasterManager.addFinanceLoanPreference(financeVO, loanPreferenceVO);

		transaction.commitTransaction();
		logMethodEnd("FinanceServicesImpl", "addFinanceLoanPreference");
	}

	@Override
	public void updateFinanceLoanPreference(LoanPreferenceVO loanPreferenceVO) {
		logMethodBegin("FinanceServicesImpl", "addFinanceLoanPreference");

		ITransactionManager transaction = new TransactionManager();
		transaction.beginTransaction();

		ILoanMasterManager loanMasterManager = getLoanMasterManager(transaction
				.getPersistenceGovernor());
		loanMasterManager.updateFinanceLoanPreference(loanPreferenceVO);

		transaction.commitTransaction();
		logMethodEnd("FinanceServicesImpl", "addFinanceLoanPreference");
	}
}
